// TOWN SCRIPT
//    Town 0: Castle Pudideum

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

int i,j,k;
string dlgstr;

body;

beginstate INIT_STATE;
	set_crime_tolerance(1);
	
	change_blocked(23,21,1);
	change_blocked(24,21,1);
	change_blocked(25,21,1);
	change_blocked(23,22,1);
	change_blocked(24,22,1);
	change_blocked(25,22,1);
	
	change_blocked(22,38,1);
	change_blocked(23,38,1);
	change_blocked(24,38,1);
	change_blocked(25,38,1);
	change_blocked(26,38,1);
	
	set_name(6,"Belial");
	set_char_dialogue_pic(6,504,0);
	set_name(7,"Scaevola");
	
	if (get_flag(6,0) != 0)
		erase_char(6);
		
	if (get_flag(0,8) != 0)
		erase_char(7);
	
	if (get_flag(250,3) != 0) {
		force_instant_terrain_redraw();
		message_dialog("The moment you set foot near Castle Putideum, you are greeted by Putidus's guards. _You have committed terrible crimes,_ they say, _and you are of no use to our master. You will be punished according to your deeds._","Before you can protest, they draw their swords and summarily execute you.");
		kill_char(1000,2,0);
		end();
		}
	
	if (get_flag(0,0) == 0) {
		force_instant_terrain_redraw();
		set_flag(250,0,2); // Umbratium destroyed
		set_flag(250,1,1); // Timoria destroyed
		set_flag(0,0,1);
		message_dialog("The biting cold wind blows across your faces as you approach the dark castle. You can hear fearsome screams from within, or so you imagine, although over the wind, you could not really distinguish with certainty.","The walls are tall and thick, but not as much as in the towns around. Lord Putidus must not fear intrusion by the animals or other forces of this area. You can only imagine why.");
		}
		
	if ((get_flag(5,2) > 0) && (get_flag(0,3) == 0)) {
		force_instant_terrain_redraw();
		set_flag(0,3,1);
		message_dialog("The guards drag you away. They take you downstairs, past the hateful grinning mouth of Belial, and they throw you bodily into a dark cell. You see movement in the cell across the hall from you, but that is all.","The walls are plain, and the bed is plain, and the gate is strong. A quick check of the walls reveals no secret passages. You are stuck here.");
		}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
if (get_flag(0,6) == 0)
	end();
if (get_flag(6,0) == 0)
	end();
	
	set_flag(0,6,1);
	
// Has not defeated Putidus
if (get_flag(250,2) == 0) {
	message_dialog("Apparently you can just walk out the front door of Castle Putideum. For whatever reason, perhaps because Putidus does not yet know that you are free, perhaps because he is willing to let you go, you may leave if you desire.","You would leave your work here quite unfinished, though. Scaevola is still in prison, Collatinus is still chained, and Lucretia's death is yet unavenged.");
	end();
	}

// Has defeated Putidus but not freed Scaevola or Collatinus
if ((get_flag(0,8) == 0) && (get_flag(5,13) == 0)) {
	message_dialog("You walk out of Castle Putideum, but then a thought strikes you: your silver key may free Scaevola or Collatinus or both. You may want to go back and do that.","");
	end();
	}
	
// Defeated Putidus and freed Collatinus, but not Scaevola
if (get_flag(0,8) == 0) {
	message_dialog("You walk out of Castle Putideum, but then you remember Scaevola, still stuck in prison as far as you know. You may wish to go back and check on him.","");
	end();
	}
	
// Defeated Putidus and freed Scaevola, but not Collatinus
if (get_flag(5,13) == 0) {
	message_dialog("You walk out of Castle Putideum, but then you remember Collatinus, still in chains as far as you know. You may wish to go back and check on him.","");
	end();
	}
	
break;

beginstate START_STATE;
if ((get_crime_level() > 0) && (get_flag(250,4) == 0)) {
		set_flag(250,4,1);
		message_dialog("You feel a chill come over you. The gods have seen your crimes, and they will certainly punish you. The only questions are when and how.","");
		}
break;

beginstate 10;
	if (get_flag(0,1) == 0) {
	reset_dialog();
	add_dialog_str(0,"Lord Putidus's castle is dark and cold, but at least you are coming out of the wind. You think you can hear it beginning to rain outside. You will be dry indoors, at least.",0);
	add_dialog_str(1,"But even as you have just finished giving thanks for your dryness indoors, you catch sight of a monster.",0);
	add_dialog_str(2,"Before you can think, you find yourself exhaling a prayer to your ancestors and your family's gods for defense against evil. This fiery beast is unlike any that you have seen.",0);
	add_dialog_str(3,"As if plucked straight from the Tartaran pit, still reeking of sulfur and red with the embers of the fiery gulf, he stands with seraphic wings outstretched, and his blazing face turns towards you.",0);
	add_dialog_str(4,"_Welcome,_ the creature says, its herald voice echoing in the fortress halls. _Welcome to Castle Putideum._",0);
	set_flag(0,1,run_dialog(1));
	}
break;

beginstate 11;
	if (get_flag(0,2) == 0) {
	set_flag(0,2,1);
	message_dialog("You push on the wall and find that it opens! You step through the secret passage and discover a strange little room. Two chairs are set up to face small holes in the walls. Through these holes you can see the entrance and the first hall.","Lord Putidus must use these peepholes to spy on his visitors.");
	}
break;

beginstate 12;
	message_dialog("On this pedestal is a note. It reads:","_That is correct. Let them in. Send them directly to me. We have nothing to fear from them, or from anyone anymore._");
break;

beginstate 13;
if (get_flag(0,4) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"As there is nothing else to do, you may sleep. Do you wish to?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Sleep in the bed.");
	if (run_dialog(1) == 2) {
		message_dialog("You descend slowly into a heavy sleep.","");
		
		force_view_center(7,26);
		force_instant_terrain_redraw();
		
		i = 0;
		while (char_ok(i) == 0)
			{i = i + 1; }
		
		reset_dialog();
		add_dialog_str(0,"You are outside the walls of Castle Putideum in the middle of the storm. Aeolus's own might pours itself upon you in drenching volumes, and you look around for shelter. None is in sight. Suddenly, out of the furious black clouds, you hear a voice:",0);
		clear_buffer();
		append_string("_");
		append_char_name(i);
		append_string(", you are trapped; if you remain where you are, Lord Putidus will kill you, but this is not your fate. You must escape._");
		get_buffer_text(dlgstr);
		add_dialog_str(1,dlgstr,0);
		add_dialog_str(2,"The voice rises, booming over the howling winds. _I will now reveal to you the arcane destiny the fates have given to you, a privilege not given to many in this fallen age._",0);
		add_dialog_str(3,"Your heart beats faster. A god is speaking to you! _You are destined in Ateria to defeat the Lord Putidus and restore proper order and degree to this region. Save the prisoners of Putideum and kill its evil master, and fulfill your destiny._",0);
		add_dialog_str(4,"A flash of lightning arcs across the sky, from the horizon to the top of the spire of Castle Putideum, and it crashes into the pinnacle with resounding thunder. First one block, then another, begin to fall from the spire, and the tower crumbles.",0);
		add_dialog_str(5,"When nothing but rubble remains, you gradually awake.",0);
		set_flag(0,4,run_dialog(1));
		
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		}
break;

beginstate 14;
if (get_flag(0,4) == 0)
	block_entry(1);
else
	set_flag(0,5,1);
break;

beginstate 15;
	reset_dialog_preset_options(9);
	if (run_dialog(1) == 2)
		move_to_new_town(6,7,15);
break;

beginstate 16;
	reset_dialog_preset_options(9);
	if (run_dialog(1) == 2)
		move_to_new_town(6,4,21);
break;

beginstate 17;
	reset_dialog();
	add_dialog_str(0,"You have come to a ladder. Do you wish to climb it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb up.");
	if (run_dialog(1) == 2)
		move_to_new_town(5,5,24);
break;

beginstate 18;
	reset_dialog_preset_options(5);
	if (run_dialog(1) == 2)
		move_to_new_town(5,24,18);
break;

beginstate 19;
if (get_flag(0,7) == 0) {
	reset_dialog();
	add_dialog_str(0,"The open codex is covered by flowing handwriting, written in a peculiarly archaic alphabet; it looks almost like a Hellene's writing. However, the words are indisputably the language of the western empire, archaic as they may be.",0);
	add_dialog_str(1,"You begin to read: _I, Putidus, Lord of Ateria, aware that certain events of my reign have been garbled by rumor, hereby set down a record of those events, that the truth may at last be known...._ By all the gods of the heavens, a personal journal?",0);
	add_dialog_str(2,"As you read, you notice that the words are fading from your sight. You hurriedly flip to a more recent page to glean whatever you can about the current situation in Ateria. Under the heading of yesterday's date:",0);
	add_dialog_str(3,"_Faustulus is of the greatest importance. Scaevola and Collatinus are impotent against me, but Faustulus I need by my side or I will fail. I need him alive and obedient. In fact, I need his trust.",0);
	add_dialog_str(4,"_I do not think that will be a problem. I rather like the man. He should not fear in the least that I will harm him._",0);
	add_dialog_str(5,"The handwriting fades entirely before you can read more.",0);
	set_flag(0,7,run_dialog(1));
	if (get_flag(5,11) != 0)
		message_dialog("What you've read puts you into a terrible bind. Faustulus will only help you if he believes that Putidus will kill him. You now know that Putidus won't.","What you do with that information is up to you.");
	}
else
	message_dialog("The book on this pedestal is now blank.","");
break;

beginstate 20;
	message_dialog("The books on these shelves cover such topics as farming, poetry, and poetry about farming. The names on these shelves are enough to make any imperial citizen proud, such as the Ecloga and Georgicon.","Like any good provincial lord, Putidus must be reading to learn the meaning of being a citizen of the empire.");
break;

beginstate 21;
if ((has_special_item(2) == 0) || (get_flag(0,8) != 0))
	end();
	
	reset_dialog();
	add_dialog_str(0,"You arrive at Scaevola's cell. Your silver key unlocks the gate. Do you wish to free him?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Free him.");
	if (run_dialog(1) == 1)
		end();
	
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	
	clear_buffer();
	append_string("Scaevola staggers out of the cell. _Thank you, ");
	append_char_name(i);
	append_string(". Putidus is dead?_ You nod. He walks gingerly toward the exit. When he reaches the door, he turns back to you.");
	get_buffer_text(dlgstr);
	message_dialog(dlgstr,"_Thank you again for your work. I must now go to restart my life._ He leaves, a fragile, broken man.");
	erase_char(7);
	set_flag(0,8,1);
	award_party_xp(50,10);
break;

beginstate 22;
	message_dialog("This room is obviously designed around kneeling at the pool and looking into it, so you do.","You see nothing in the murky water.");
break;